package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.Topic;
import com.cskaoyan.mall.bean.TopicExample;
import com.cskaoyan.mall.mapper.TopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;

    @Override
    public HashMap<String, Object> queryTopicList(Integer page, Integer limit, String sort, String order,String title,String subtitle) {
        PageHelper.startPage(page,limit);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (title != null){
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null){
            criteria.andTitleLike("%" + subtitle + "%");
        }
        topicExample.setOrderByClause(sort + " " + order);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("items",topics);
        return map;
    }

    @Override
    public Topic insertTopic(Topic topic) {
        topicMapper.insert(topic);
        Topic topicVo = topicMapper.selectLastInsertTopic();
        return topicVo;

    }

    @Override
    public Topic updateTopic(Topic topic) {
        topicMapper.updateTopic(topic);
        Topic topicVo = topicMapper.selectByPrimaryKey(topic.getId());
        return topicVo;
    }

    @Override
    public void deleteTopic(Integer id) {
        topicMapper.deleteTopic(id);
    }
}
